﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using LNUGame.Model;
using LNUGame.View;

namespace LNUGame.Controller
{
    /// <summary>
    /// Controls everything in the game
    /// </summary>
    class MainController
    {
        private GameController m_gameController;
        private MenuController m_menuController;
        private IController m_currentController;

        private GraphicsDeviceManager m_manager;
        private ContentManager m_contentManager;

        /// <summary>
        /// Main constructor.
        /// Creates a new MenuController to allow the user to navigate the main menu.
        /// </summary>
        /// <param name="a_manager">The GraphicsDeviceManager to use</param>
        /// <param name="a_contentManager">The ContentManager to use</param>
        public MainController(GraphicsDeviceManager a_manager, ContentManager a_contentManager)
        {
            m_menuController = new MenuController(a_manager, a_contentManager);

            m_currentController = m_menuController;

            m_manager = a_manager;
            m_contentManager = a_contentManager;
        }

        /// <summary>
        /// Monitors choices made in the other controllers.
        /// The the user starts a new game the current controller is exchanged for a new GameController
        /// </summary>
        /// <param name="a_elapsedTime">How long time since the game was started</param>
        internal void Update(float a_elapsedTime)
        {
            m_currentController.Update(a_elapsedTime);
            if(m_currentController.StartNewGame())
            {
                NewGame();
            }

            if (m_currentController.DidPlayerChangeController() == true)
            {
                if (m_currentController == m_gameController)
                {
                    m_currentController = m_menuController;
                }
                else
                {
                    m_currentController = m_gameController;
                }
            }
        }

        /// <summary>
        /// Draws the current controller
        /// </summary>
        /// <param name="a_elapsedTime">How long time since the game was started</param>
        /// <param name="a_graphicsDevice">The GraphicsDevice to use</param>
        internal void Draw(float a_elapsedTime, GraphicsDevice a_graphicsDevice)
        {
            m_currentController.Draw(a_elapsedTime, a_graphicsDevice);
        }

        /// <summary>
        /// Creates a new GameController to start a new game
        /// </summary>
        private void NewGame()
        {
            m_gameController = new GameController(m_manager, m_contentManager);
        }
    }
}
